AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

function ENT:SpawnFunction(ply, tr)
	local SpawnPos = tr.HitPos + tr.HitNormal * 42

	local ent = ents.Create(self.ClassName)
	ent:SetPos(SpawnPos)
	ent.dt.Owner = ply
	ent:Spawn()
	ent:Activate()
		
	return ent
end

function ENT:Initialize()
	self:SetModel(self.Model)
	local c = self.Color
	self:SetColor(c.r, c.g, c.b, c.a)
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:PhysWake()
	
	self.LastUsed = CurTime()
end

function ENT:Use(activator, caller)
	if self.LastUsed > CurTime() then return end
	
	if (self.LastUsed + 0.3 < CurTime()) then
		self.LastUsed = CurTime() - 0.1
		self.dt.Use = 100
	end
	self.LastUsed = CurTime() + 0.1
	self.dt.Use = self.dt.Use - 10
	if (self.dt.Use <= 0) then
		self:EmitSound("ItemBattery.Touch")
		self:GiveItemEffects(activator)
		self:Remove()
	end
end

function ENT:OnTakeDamage(dmg)
	if self.HP then
		self.HP = self.HP - dmg:GetDamage()

		if self.HP <= 0 then
			local effectdata = EffectData()
				effectdata:SetOrigin(self:GetPos())
				effectdata:SetMagnitude(2)
				effectdata:SetScale(2)
				effectdata:SetRadius(3)
			util.Effect("Sparks", effectdata)
			
			self:Remove()
		end
	end
end

function ENT:CanRecycle()
	return true
end
